package com.jinke.uang.model;

import android.graphics.Bitmap;

import com.jinke.uang.BuildConfig;
import com.jinke.uang.bean.IdentityData;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.common.UserAuthRouter;
import com.jinke.uang.common.UserInfo;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.http.ModelUtils;
import com.jinke.uang.http.ParamsFactory;
import com.jinke.uang.http.RetrofitManager;
import com.jinke.uang.interfaces.ICommonModule;
import com.jinke.uang.interfaces.ICommonView;
import com.jinke.uang.utils.toolutil.ImageUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 作者：Song Shuyu
 * 时间：2019/4/11 16:18
 * package is com.jinke.uang.model
 * 认证相关的网络数据请求接口
 */
public class AuthModule implements ICommonModule {
    @Override
    public void getData(ICommonView view, int apiType,String listType ,Object[] t) {
        Map<String, Object> paramsMap = new HashMap<>();
        switch (apiType) {

            case HttpConstant.AUTH_STATUS://获取认证状态接口
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().getAuthStatus(UserInfo.getToken(),
                        ParamsFactory.getCommonParams(null)), view, apiType,listType, t);
                break;

            case HttpConstant.VERIFY_DATA_STATUS://验证数据的状态
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().getVerifyDataStatus(BuildConfig.channel, UserInfo.getToken(),
                        ParamsFactory.getCommonParams(null)), view, apiType,listType, t);
                break;

            case HttpConstant.AUTH_INFO_DATA: //单位认证，联系人认证，图片认证接口
                paramsMap.put("userUuid", UserInfo.getShowUuid());
                paramsMap.put("authPageCode", String.valueOf(t[0]));
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().getAuthInfoData(UserInfo.getToken(), ParamsFactory
                        .getCommonParams(paramsMap)), view, apiType,listType, t);
                break;

            case HttpConstant.MONEY_AUTH_INFO_DATA:  //借款金额认证，个人资信认证
                paramsMap.put("userUuid", UserInfo.getShowUuid());
                paramsMap.put("pageCodes", String.valueOf(t[0]));
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().getAuthInfoData(UserInfo.getToken(), ParamsFactory
                        .getCommonParams(paramsMap)), view, apiType,listType, t);
                break;

            case HttpConstant.SAVE_PERSONAL_AUTH: //提交单位信息接口
                paramsMap.clear();
                paramsMap.put("authPageCode", UserAuthRouter.getInstance().routerMap2Code(RouterMap
                        .WORK_AUTH_PAGE));
                paramsMap.put("companyName", String.valueOf(t[0]));
                paramsMap.put("companyIndustryCode", String.valueOf(t[1]));
                paramsMap.put("commonProfessional", String.valueOf(t[2]));
                paramsMap.put("workStartTime", String.valueOf(t[3]));
                paramsMap.put("salary", String.valueOf(t[4]));
                paramsMap.put("companyTel", String.valueOf(t[5]));
                paramsMap.put("companyNet", String.valueOf(t[6]));
                paramsMap.put("companyProvince", String.valueOf(t[7]));
                paramsMap.put("companyCity", String.valueOf(t[8]));
                paramsMap.put("companyTown", String.valueOf(t[9]));
                paramsMap.put("companyRegion", String.valueOf(t[10]));
                paramsMap.put("companyAddressDesc", String.valueOf(t[11]));
                paramsMap.put("companyZipcode", String.valueOf(t[12]));
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("authPageCode",String.valueOf(t[13]));
                if (UserInfo.isUserInformationWrong()) {
                    paramsMap.put("isInformationWrong", 1);
                }
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().savePersonalAuth(UserInfo.getToken(), ParamsFactory
                        .getCommonParams(paramsMap)), view, apiType,listType, t);
                break;

            case HttpConstant.MONEY_SAVE_PERSONAL_AUTH: //提交借款用途信息接口
                paramsMap.clear();
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("needMoney", String.valueOf(t[0]));
                paramsMap.put("moneyTodo", String.valueOf(t[1]));
                paramsMap.put("authPageCode", String.valueOf(t[2]));
                if (UserInfo.isUserInformationWrong()){
                    paramsMap.put("isInformationWrong", 1);
                }
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().savePersonalAuth(UserInfo.getToken(), ParamsFactory
                        .getCommonParams(paramsMap)), view, apiType,listType, t);
                break;

            case HttpConstant.PERSONAL_SAVE_PERSONAL_AUTH://提交个人信息接口
                paramsMap.clear();
                paramsMap.put("birthday", String.valueOf(t[0]));
                paramsMap.put("education", String.valueOf(t[1]));
                paramsMap.put("email", String.valueOf(t[2]));
                paramsMap.put("motherName", String.valueOf(t[3]));
                paramsMap.put("familyNum", String.valueOf(t[4]));
                paramsMap.put("sonsNum", String.valueOf(t[5]));
                paramsMap.put("province", String.valueOf(t[6]));
                paramsMap.put("city", String.valueOf(t[7]));
                paramsMap.put("town", String.valueOf(t[8]));
                paramsMap.put("region", String.valueOf(t[9]));
                paramsMap.put("address", String.valueOf(t[10]));
                paramsMap.put("zipCode", String.valueOf(t[11]));
                paramsMap.put("bigRegionCode", String.valueOf(t[12]));
                paramsMap.put("smallRegionCode", String.valueOf(t[13]));
                paramsMap.put("residenceStartTime", String.valueOf(t[14]));
                paramsMap.put("homeowner", String.valueOf(t[15]));
                paramsMap.put("rentalScopeCode", String.valueOf(t[16]));
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("authPageCode", String.valueOf(t[17]));
                if (UserInfo.isUserInformationWrong()) {
                    paramsMap.put("isInformationWrong", 1);
                }
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().savePersonalAuth(UserInfo.getToken(), ParamsFactory
                        .getCommonParams(paramsMap)), view, apiType,listType, t);
                break;

            case HttpConstant.UPLOAD_CONTACT_DATA://提交通讯录信息接口
                paramsMap.clear();
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("authPageCode", String.valueOf(t[0]));
                paramsMap.put("paramList", t[1]);
                if (UserInfo.isUserInformationWrong()) {
                    paramsMap.put("isInformationWrong", 1);
                }
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().uploadContactData(UserInfo.getToken(), ParamsFactory
                        .getCommonParams(paramsMap)), view, apiType,listType, t);
                break;

            case HttpConstant.UPLOAD_SINGLE_IMG:  //上传图片接口
                paramsMap.clear();
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("fileType", String.valueOf(t[0]));

                HashMap<String, Object> map = new HashMap<>();
                map.put("paramStr", ParamsFactory.getCommonParamsStr(paramsMap));
                RequestBody requestBody = FormBody.create(MediaType.parse("multipart/form-data"), (File)t[1]);
                MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", "file", requestBody);

                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().uploadSingleImg(UserInfo.getToken(),
                        map,filePart), view, apiType,listType, t);
                break;

                case HttpConstant.BITMAP_UPLOAD_SINGLE_IMG:  //压缩之后上传图片接口
                paramsMap.clear();
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("fileType", String.valueOf(t[0]));

                HashMap<String, Object> bitmapMap = new HashMap<>();
                bitmapMap.put("paramStr", ParamsFactory.getCommonParamsStr(paramsMap));
                RequestBody requestBody1 = FormBody.create(MediaType.parse("multipart/form-data"), (File)t[1]);
                MultipartBody.Part filePart1 = MultipartBody.Part.createFormData("file", "file", requestBody1);
                    ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().uploadSingleImg(
                            UserInfo.getToken(),bitmapMap,filePart1), view, apiType,listType, t);
                break;

            case HttpConstant.SAVE_ADD_INFORMATION:
                paramsMap.clear();
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("authPageCode", String.valueOf(t[0]));
                paramsMap.put("imageList", (t[1]));
                if (UserInfo.isUserInformationWrong()) {
                    paramsMap.put("isInformationWrong", 1);
                }
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().saveAddInformation(UserInfo.getToken(), ParamsFactory
                        .getCommonParams(paramsMap)), view, apiType,listType, t);
                break;

            case HttpConstant.SAVE_IDENTITY:
                //保存用户信息
                paramsMap.clear();
                IdentityData.IdentityBean identity= ((IdentityData.IdentityBean) t[0]);
                Map<String, Object> toMap = identity.toMap();
                toMap.put("authPageCode",String.valueOf(t[2]));
                toMap.put("userUuid", UserInfo.getSaveUuid());
                toMap.put("imageInfoParams",(List<IdentityData.ImageInfoBean>)(t[3]));
                toMap.put("pass", String.valueOf(t[4]));
                if (UserInfo.isUserInformationWrong()) {
                    toMap.put("isInformationWrong", 1);
                }
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().saveIdentity(UserInfo.getToken(), ParamsFactory
                        .getCommonParams(toMap)), view, apiType,listType, t);
                break;

            case HttpConstant.IMG_COMPARE:
                paramsMap.clear();
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("firstImagePath", String.valueOf(t[0]));
                paramsMap.put("secondImagePath", String.valueOf(t[1]));
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().imgCompare(UserInfo.getToken(), ParamsFactory
                        .getCommonParams(paramsMap)), view, apiType,listType, t);
                break;

            case HttpConstant.UPLOAD_IDENTITY_CARD: //上传身份证图片信息
                paramsMap.clear();
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("fileType", String.valueOf(t[0]));
                HashMap<String, Object> map2 = new HashMap<>();
                map2.put("paramStr", ParamsFactory.getCommonParamsStr(paramsMap));
                byte[] bitmapByte = ImageUtils.bitmap2Bytes((Bitmap) t[1], Bitmap.CompressFormat.JPEG);
                RequestBody requestBody2 = FormBody.create(MediaType.parse("multipart/form-data"),
                        bitmapByte);
                MultipartBody.Part filePart2 = MultipartBody.Part.createFormData("file", "file",
                        requestBody2);
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().uploadIdentityCard(UserInfo.getToken(),
                        map2,filePart2), view, apiType,listType, t);
                break;

            case HttpConstant.UPLOAD_IDENTITY_CARD_ONE:
                paramsMap.clear();
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("fileType", "1");

                HashMap<String, Object> map3 = new HashMap<>();
                map3.put("paramStr", ParamsFactory.getCommonParamsStr(paramsMap));

                RequestBody requestBody3 = FormBody.create(MediaType.parse("multipart/form-data"), (File)(t[0]));
                MultipartBody.Part filePart3 = MultipartBody.Part.createFormData("file", "file", requestBody3);
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().uploadIdentityCard(UserInfo.getToken(),
                        map3,filePart3), view, apiType,listType, t);
                break;

            case HttpConstant.DICTIONARY_DATA://获取银行卡字典数据
                paramsMap.clear();
                paramsMap.put("dictType", String.valueOf(t[0]));

                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().
                        getDictionaryData(BuildConfig.channel, UserInfo.getToken(), ParamsFactory.getCommonParams(paramsMap)), view, apiType, listType,t);
                break;

            case HttpConstant.PERSON_INFO://用户信息验证回显接口
                paramsMap.put("userUuid", UserInfo.getShowUuid());
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().
                        getPersonInfo(BuildConfig.channel, UserInfo.getToken(), ParamsFactory.getCommonParams(paramsMap)), view, apiType, listType,t);
                break;

            case HttpConstant.SAVE_BANK_CARD_INFO://添加银行卡接口
                paramsMap.clear();
                paramsMap.put("userUuid", UserInfo.getSaveUuid());
                paramsMap.put("realName", String.valueOf(t[0]));
                paramsMap.put("bankCode", String.valueOf(t[1]));
                paramsMap.put("bankName", String.valueOf(t[2]));
                paramsMap.put("bankCard", String.valueOf(t[3]));
                paramsMap.put("authPageCode", "bank_info_code");
                if (UserInfo.isUserInformationWrong()) {
                    paramsMap.put("isInformationWrong", 1);
                }
                ModelUtils.ModelUtils(RetrofitManager.getInstance().getINetService().
                        saveBankCardInfo(UserInfo.getToken(), ParamsFactory.getCommonParams(paramsMap)), view, apiType, listType,t);
                break;

            default:
                    break;
        }
    }
}